package DemoATM;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class ATM {
    Scanner sc=new Scanner(System.in);
    Random r=new Random();
    ArrayList<Account> accounts=new ArrayList<>();
    public void start(){
        while (true){
            System.out.println("---------欢迎进入ATM系统----------");
            System.out.println("1:登录");
            System.out.println("2：开户");
            System.out.println("请输入你的操作命令：");
            char a=sc.next().charAt(0);
            switch (a){
                case '1':
                    int m=log();
                    if(m==-1){
                        break;
                    }
                    operator(m);
                    break;
                case '2':
                    creatAccount();
                    break;
                default:
                    System.out.println("没有该操作！");
            }
        }
    }
    private void operator(int m){
        String name1=accounts.get(m).getName().substring(0,1);
        String name2=accounts.get(m).getGender()=='男'?"先生":"女士";
        System.out.println("----欢迎"+name1+name2+"进入业务系统----");
        while (true){
            System.out.println("--------------------------------");
            System.out.println("1：查询当前账户");
            System.out.println("2：取款");
            System.out.println("3：存款");
            System.out.println("4：转账");
            System.out.println("5：密码修改");
            System.out.println("6：退出");
            System.out.println("7；注销当前账户");
            System.out.println("请输入你要操作的业务：");
            int n= sc.nextInt();
            switch (n){
                case 1:
                    System.out.println("---------------------");
                    System.out.println("当前余额："+accounts.get(m).getMoney());
                    System.out.println("单笔限额："+accounts.get(m).getLimit());
                    break;
                case 2:
                    System.out.println("-------------------");
                    while (true){
                        System.out.println("请输入你要取的金额：");
                        double money=sc.nextDouble();
                        if(money>accounts.get(m).getLimit()){
                            System.out.println("单笔取款超出限额！");
                            break;
                        }
                        else if(accounts.get(m).getMoney()<money){
                            System.out.println("余额不足！");
                            break;
                        }
                        else{
                            accounts.get(m).setMoney(accounts.get(m).getMoney()-money);
                            System.out.println("取款成功~~~");
                            break;
                        }
                    }
                    break;
                case 3:
                    System.out.println("-------------------");
                    System.out.println("请输入你要存款的金额：");
                    double money=sc.nextDouble();
                    accounts.get(m).setMoney(money);
                    System.out.println("存款成功~~~");
                    break;
                case 4:
                    System.out.println("-------------------");
                    exit:
                    while (true){
                        System.out.println("请输入想要转入账户的卡号：");
                        String id= sc.next();
                        if(Isexist(id)==-1){
                            System.out.println("该卡号不存在！");
                            continue exit;
                        }
                        int m1=Isexist(id);
                        while (true){
                            System.out.println("请输入你要转账的金额：");
                            double money1=sc.nextDouble();
                            if(money1>accounts.get(m).getLimit()){
                                System.out.println("超出限额！");
                                break;
                            }
                            else if(accounts.get(m).getMoney()<money1){
                                System.out.println("余额不足！");
                                break;
                            }
                            else{
                                accounts.get(m1).setMoney(accounts.get(m1).getMoney()+money1);
                                accounts.get(m1).setMoney(accounts.get(m).getMoney()-money1);
                                System.out.println("转账成功~~~");
                                break;
                            }
                        }
                        break ;
                    }
                    break;
                case 5:
                    System.out.println("-------------------");
                    exit:
                    while (true){
                        System.out.println("请输入原密码：");
                        String k1= sc.next();
                        if(!accounts.get(m).getPassWord().equals(k1)){
                            System.out.println("密码错误！");
                            continue exit;
                        }
                        System.out.println("请输入修改后的密码：");
                        String k2=sc.next();
                        accounts.get(m).setPassWord(k2);
                        System.out.println("密码修改成~~~");
                        break ;
                    }
                    break;
                case 6:
                    return;
                case 7:
                    System.out.println("-------------------");
                    int i=0;
                    exit:
                    while (true){
                        System.out.println("请输入你的真实姓名：");
                        String sure1= sc.next();
                        System.out.println("请输入你的密码：");
                        String sure2=sc.next();
                        if(accounts.get(m).getName().contains(sure1)&&accounts.get(m).getPassWord().equals(sure2)){
                            accounts.remove(m);
                            System.out.println("注销成功~~~");
                            return;
                        }
                        else{
                            System.out.println("姓名或密码错误！");
                            if(i==3) break ;
                            i++;
                            continue exit;
                        }
                    }
                    break;
                default:
                    System.out.println("没有该业务！");
            }
        }
    }
    public void creatAccount(){
        Account a=new Account();
        System.out.println("--------开户--------");
        while (true){
            System.out.println("请输入你的用户名：");
            String s1= sc.next();
            a.setName(s1);
            System.out.println("请输入你的密码：");
            String s2=sc.next();
            System.out.println("请再次输入你的密码：");
            String s3=sc.next();
            if(s2.equals(s3)){
                a.setPassWord(s2);
                break;
            }
            else System.out.println("密码错误！");
        }
        while (true){
            System.out.println("请输入你的性别：");
            String s4= sc.next();
            if(s4.equals("男")){
                a.setGender('男');
                break;
            } else if (s4.equals("女")) {
                a.setGender('女');
                break;
            }else System.out.println("输入不规范！");
        }
        System.out.println("输入你的取现额度：");
        double limit =sc.nextDouble();
        a.setLimit(limit);
        String id=creatId();
        a.setId(id);
        accounts.add(a);
        System.out.println("恭喜"+a.getName()+"开户成功，你的卡号为："+id);
    }
    public String creatId(){
        while(true){
            String s="";
            for (int i = 0; i < 8; i++) {
                s+=r.nextInt(10);
            }
            if (Isexist(s)==-1){
                return s;
            }
            else continue;
        }
    }
    private int Isexist(String id){
        if(accounts.size()==0){
            return -1;
        }
        for (int i = 0; i < accounts.size(); i++) {
            if (accounts.get(i).getId().equals(id)){
                return i;
            }
        }
        return -1;
    }
    public int log(){
        while(true){
            System.out.println("请输入你的卡号：");
            String s1=sc.next();
            if(Isexist(s1)==-1){
                System.out.println("该卡号不存在！");
                return -1;
            }
            System.out.println("请输入你的密码：");
            String s2=sc.next();
            int m=Isexist(s1);
            if (accounts.get(m).getPassWord().equals(s2)){
                System.out.println("登录成功~~~");
                return  m;
            }
            else System.out.println("密码错误！");
        }
    }
}
